package light.mvc.service.books.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.books.Tmaterial;
import light.mvc.model.sys.Tresource;
import light.mvc.model.sys.Tuser;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.base.Tree;
import light.mvc.pageModel.books.Material;
import light.mvc.pageModel.sys.Resource;
import light.mvc.service.books.VideoServiceI;
import light.mvc.service.sys.ResourceServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VideoServiceImpl implements VideoServiceI {

	@Autowired
	private BaseDaoI<Tmaterial> materialDao;
	@Autowired
	private BaseDaoI<Tuser> userDao;

	@Override
	public List<Material> treeGrid() {
		List<Material> lr = new ArrayList<Material>();
		return lr;
	}

	@Override
	public void add(Material material) {
		Tmaterial t = new Tmaterial();
		Tmaterial parent = materialDao.get(Tmaterial.class, material.getParentId());
		Tuser tuser = userDao.get(Tuser.class, material.getBuilderId());
		t.setParent(parent);
		t.setUrl(material.getUrl());
		t.setName(material.getName());
		t.setBuilder(tuser);
		BeanUtils.copyProperties(material, t);
		materialDao.save(t);
	}

	@Override
	public void delete(Long id) {
		//刪除数据库中的记录
		Tmaterial t = materialDao.get(Tmaterial.class, id);
		materialDao.delete(t);
		
		//删除实际文件
		String url = t.getUrl();
		File file = new File(url);
		if (file.exists()) {
			file.delete();
		}
	}

	@Override
	public void edit(Material video) {
		// TODO Auto-generated method stub

	}

	@Override
	public Material get(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Tree> tree() {

		return null;
	}

	@Override
	public List<Tree> allTree(boolean flag,String id) {
		List<Tree> lr = new ArrayList<Tree>();
		List<Tmaterial> l = null;
		if (id != null) {
//			Tmaterial tParent = materialDao.get("from Tmaterial where t.id = " + id);
//			l = materialDao.find("from Tmaterial where t.parent.id =" + Long.valueOf(id));
			l = materialDao.findTmaterialBySql("select * from lib_materials t where t.pid = " + id );
		}else{
			l = materialDao.find("from Tmaterial");
		}
		if ((l != null) && (l.size() > 0)) {
			for (Tmaterial r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				if (r.getParent() != null) {
					tree.setPid(r.getParent().getId().toString());
				}
				tree.setText(r.getName());
				if (r.getState() != null) {
					tree.setState(r.getState());
				}
				if (r.getParent() != null) {
					tree.setPid(r.getParent().getId().toString());
				}
				tree.setDescription(r.getDescription());
				tree.setAttributes(new String(r.getUrl() + "&" + r.getMaterialType()));
				lr.add(tree);
			}
		}
		return lr;

	}

	@Override
	public List<String> resourceAllList() {
		// TODO Auto-generated method stub
		return null;
	}
}
